package com.smbms.controller;

import com.smbms.pojo.User;
import com.smbms.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Resource
    private UserService userService;

    @RequestMapping(value = "/login.html")
    public String toLogin(){
        return "login";
    }

    @RequestMapping(value = "/login.html",method = RequestMethod.POST)
    public String doLogin(String userCode, String userPassword, Model model
            , HttpSession session, HttpServletRequest request) throws Exception {

        User user = userService.login(userCode,userPassword);
        if (user == null){
//            model.addAttribute("error","用户名或密码错误");
            request.setAttribute("error","用户名或密码错误");
            return "login";
        } else {
            session.setAttribute("userSession",user);
            return "redirect:/main.html";
        }
    }

    @RequestMapping(value = "/main.html")
    public String main(){
        return "frame";
    }
}
